package com.tajiang.business.businesscount.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.adapter.ViewHolder;
import com.tajiang.business.common.application.TJApp;
import com.tajiang.business.common.utils.PostDataTools;
import com.tajiang.business.model.BankInfo;
import com.tajiang.business.order.adapter.CommomAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 选择银行
 */
public class SelectBankActivity extends BaseActivity {

    @BindView(R.id.lv_public)
    ListView listView;

    private CommomAdapter<BankInfo> commomAdapter;
    private List<BankInfo> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTopBar() {
        setTitle("选择银行");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_select_bank);
    }

    @Override
    protected void initData() {

        listView.setDividerHeight(0);
        listView.setAdapter(commomAdapter = new CommomAdapter<BankInfo>(this, dataList, R.layout.item_layout_bank) {

            @Override
            public void convert(ViewHolder helper, BankInfo item) {

                TJApp.getImageLoader().displayImage(item.getIcon(), (ImageView) helper.getView(R.id.item_bank_icon), TJApp.getOptionsErrorPic());
                helper.setText(R.id.item_bank_name, item.getName());
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                intent.putExtra("bankId", dataList.get(position).getBankId());
                intent.putExtra("bankName", dataList.get(position).getName());
                setResult(RESULT_OK, intent);

                dataList.clear();
                dataList = null;
                finish();
            }
        });

        PostDataTools.bank_list(this, new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                dataList.clear();;
                dataList.addAll((List<BankInfo>) object);

                commomAdapter.notifyDataSetChanged();
            }
        });
    }


}
